package com.huweilong.tingshu.servlet.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huweilong.tingshu.util.Log;
import com.huweilong.tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 是阿秋啊
 * @date 2022/08/03 20:10
 **/
@WebServlet("/studio/user/currentuser.json")
public class CurrentuserJsonServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession(false);
        UserVO userVO = new UserVO();
        if (session != null) {
            userVO = (UserVO) session.getAttribute("currentUser");
        }
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        String json = objectMapper.writeValueAsString(userVO);
        Log.println("发送响应体"+json);
        resp.getWriter().println(json);
    }
}